//
//  ShellMilestoneView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellMilestoneView.h"
#import "ShellHomeViewModel.h"
#import "ShellMoreMilestoneView.h"
#import "ShellMilestoneModel.h"

@interface  ShellMilestoneView()

@property (nonatomic,strong) ShellHomeViewModel *viewModel;

//里程碑
@property (nonatomic,strong) UILabel *milestoneLab;

@end

@implementation ShellMilestoneView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.cornerRadius = 6;
        self.backgroundColor = UIColorHex(0x08233F);
        self.viewModel = (ShellHomeViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    UILabel *titleLab = [UILabel lz_labelWithText:LZLocalizedString(@"里程碑") fontSize:k_textFont(14) color:UIColorHex(0x156097)];
    [self addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(22);
    }];
    
    UIView *quanView = [[UIView alloc] init];
    quanView.bColor = UIColorHex(0x4AB2E7);
    quanView.bWidth = 2;
    quanView.cornerRadius = 5;
    [self addSubview:quanView];
    [quanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(17);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(10);
    }];
    
    [self addSubview:self.milestoneLab];
    [self.milestoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(quanView.mas_top).mas_offset(-3);
        make.left.mas_equalTo(quanView.mas_right).mas_offset(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(17);
    }];
    
  
    UIImageView *lineView = [[UIImageView alloc] init];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerX.mas_equalTo(quanView.mas_centerX);
      make.top.mas_equalTo(quanView.mas_bottom).mas_offset(5);
      make.width.mas_equalTo(1);
      make.bottom.mas_equalTo(self.milestoneLab.mas_bottom).mas_offset(25);
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [self drawLineOfDashByCAShapeLayer:lineView lineLength:1 lineSpacing:3 lineColor:UIColorHex(0xB3BED9) lineDirection:NO];
    });

    
    UIButton *nextBtn = [UIButton lz_buttonImageName:@"icon_next" backgroundImageName:@""];
    [self addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(24);
    }];
    
    @weakify(self);
    [[nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [ShellMoreMilestoneView show];
    }];
    
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        
        if([x isKindOfClass:[NSNumber class]]){
            if([x integerValue] == 1){
               ShellMilestoneModel *milestoneModel = self.viewModel.milestoneArray.firstObject;
               NSString *milestoneString = [NSString stringWithFormat:@"%@ %@",milestoneModel.time?:@"" ,milestoneModel.desc?:@""];
               NSMutableAttributedString *milestoneAttributedString = [NSAttributedString attributedString:milestoneString rangeTitle:milestoneModel.time leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
               self.milestoneLab.attributedText = milestoneAttributedString;
               
               CGFloat milestoneHeight = [self.milestoneLab sizeThatFits:CGSizeMake(K_SCREENWIDTH - 30 - 50 - 20, MAXFLOAT)].height;
               [self.milestoneLab mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(milestoneHeight);
                }];
               
               [self mas_updateConstraints:^(MASConstraintMaker *make) {
                   make.height.mas_equalTo(milestoneHeight + 125);
               }];
            }
        }

        
       
    }];
    
    //翻译
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        titleLab.text = LZLocalizedString(@"里程碑");
    }];
    
}

/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    if (isHorizonal) {

        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];

    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);

    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}


#pragma mark - get && set

- (UILabel *)milestoneLab{
    if(!_milestoneLab){
        _milestoneLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0xFFFFFF)];
    }
    return _milestoneLab;
}



@end
